﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HtmlMultiFileEditor.Model
{
    public class Manager
    {

        #region "Singleton"

        private static Manager manager = new Manager();

        private Manager() { }

        public static Manager Instance
        {

            get { return manager; }
        }

        #endregion

        #region "Enums"

        public enum ModifyMode
        {
            Current,
            Selection,
            All

        }

        public enum PreviewMode
        {

            Enable,
            Disable

        }

        public enum ReplacementMode
        {
            Direct,
            Id,
            Class,
            Tag,
            XPath


        }

        #endregion

        #region "Events"

        public delegate void ModificationEventHandler(object sender, string eCurrentFile);
        public event ModificationEventHandler FileModified;

        #endregion

        #region "Private"

        private string message = String.Empty;

        private PreviewMode preview = PreviewMode.Enable;

        private ReplacementMode replacement = ReplacementMode.Direct;

        private void ModifyFile(string file, ReplacementMode rMode, string replacement, string replaceWith)
        {

            string currentFileContent = OpenFile(file);
            if (rMode == ReplacementMode.Direct)
            {
                SaveFile(file, currentFileContent.Replace(replacement, replaceWith));

            }
            else if (rMode == ReplacementMode.Id)
            {
                HtmlPage webpage = new HtmlPage(file);
                HtmlAgilityPack.HtmlNode node = webpage.GetElementById(replacement);
                node.InnerHtml = replaceWith;
                webpage.Document.Save(file);

            }
            else if (rMode == ReplacementMode.XPath)
            {

                HtmlPage webpage = new HtmlPage(file);
                HtmlAgilityPack.HtmlNodeCollection elementCollection = webpage.GetElementByXPath(replacement);
                foreach (var element in elementCollection)
                {
                    element.InnerHtml = replaceWith;
                }
                webpage.Document.Save(file);
            }
        }

        #endregion

        #region "Public"

        public PreviewMode Preview
        {
            get { return preview; }
            set { preview = value; }
        }

        public ReplacementMode Replacement
        {
            get { return replacement; }
            set { replacement = value; }
        }

        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        public void Modify(ModifyMode mMode, ReplacementMode rMode, string replacement, string replaceWith)
        {

            if (mMode == ModifyMode.Current)
            {
                ModifyFile(FileHandler.Instance.CurrentFile, rMode, replacement, replaceWith);

            }
            else if (mMode == ModifyMode.Selection)
            {
                foreach (var file in FileHandler.Instance.FileListSelected)
                {
                    ModifyFile(file, rMode, replacement, replaceWith);
                }

            }

            else if (mMode == ModifyMode.All)
            {
                foreach (var file in FileHandler.Instance.FileList)
                {
                    ModifyFile(file, rMode, replacement, replaceWith);

                }
            }

            if (FileModified != null)
                FileModified(this, FileHandler.Instance.CurrentFile);
        }

        public string OpenFile(string filePath)
        {
            return System.IO.File.ReadAllText(filePath);
        }

        public void SaveFile(string filePath, string content)
        {
            System.IO.File.WriteAllText(filePath, content);

        }

        #endregion

    }
}
